<template>
  <el-dialog
    v-bind="$attrs"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :title="dialogTitle"
    v-on="$listeners"
    @open="onOpen"
    @close="onClose"
  >
    <el-form ref="elForm" :model="formData" size="medium" label-width="100px">
      <el-row>
        <el-col :span="5">
          <el-form-item label="是否可用" prop="status" required>
            <el-switch v-model="formData.status" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item
            label-width="110px"
            label="接口权限拦截"
            prop="appIntercept"
            required
          >
            <el-switch v-model="formData.appIntercept" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="登录拦截" prop="loginIntercept" required>
            <el-switch v-model="formData.loginIntercept" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="用户权限" prop="permsIntercept" required>
            <el-switch v-model="formData.permsIntercept" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          placeholder="请输入备注"
          :autosize="{ minRows: 4, maxRows: 6 }"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  inheritAttrs: false,
  props: {
    dialogTitle: {
      type: String
    }
  },
  data() {
    return {
      formData: {
        status: true,
        appIntercept: true,
        loginIntercept: true,
        permsIntercept: true,
        remark: undefined
      },
      dialogVisible: false
    };
  },
  methods: {
    onOpen() {},
    onClose() {
      this.$refs["elForm"].resetFields();
    },
    close() {
      this.$emit("update:visible", false);
      this.dialogVisible = false;
    },
    handelConfirm() {
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        const result = JSON.parse(JSON.stringify(this.formData));
        ["appIntercept", "loginIntercept", "permsIntercept", "status"].forEach(
          item => {
            result[item] = Number(result[item]);
          }
        );
        this.$emit("edit", result);
      });
    }
  }
};
</script>
<style></style>
